<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>axios 调用方法发送请求</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.26.1/axios.min.js"></script>
</head>
<body>
    <button>get</button>
    <button>post</button>
    <button>put</button>
    <button>delete</button>

    <script>
        let btns = document.querySelectorAll('button');

        //绑定事件
        btns[0].onclick = async function(){
            // axios.get(url, config);
            let res = await axios.get('http://127.0.0.1:3000/movie'); // .get 发送 get 请求
            console.log(res);
        }

        //
        btns[1].onclick = async function(){
            // axios.post(url, data, config);
            let res = await axios.post('http://127.0.0.1:3000/movie', {name: '指环王', director: '外国地'})// 发送 post 请求
            console.log(res);
        }
    
        //发送 put 请求
        btns[2].onclick = async function(){
            // put/patch 与 post 请求参数格式是一致的
            let result = await axios.put(
                        'http://127.0.0.1:3000/movie/6', 
                        {name: '哈利波特', director: '国外的导演'}
                    );
            console.log(result);
        }

        //发送 delete 请求
        btns[3].onclick = async function(){
            //delete 请求与 get 请求参数格式是一致的
            let result = await axios.delete('http://127.0.0.1:3000/movie/39');
            console.log(result);
        }
    </script>
</body>
</html>